package labelsystem.Controller;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/8 17:42
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import labelsystem.Pojo.*;
import labelsystem.Service.yuserService;
import labelsystem.utils.monitorHandle;
import labelsystem.utils.returnMSG;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/8 17:42
 *@Version 1.0
 */
@Api("用户处理模块")
@RestController
@CrossOrigin
public class yUserHandler {
    @Resource
    yuserService yuserService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping("/loginPasswordY")
    @ApiOperation("用户登录:phone,password")
    public returnMSG loginInPassword(@RequestParam("phone") String phone,@RequestParam("password") String password){
           boolean flag=yuserService.loginInByPassword(phone,password);
           YoungUser youngUser=yuserService.queryInfoBytel(phone);
           if(flag){
               returnMSG returnMSG= labelsystem.utils.returnMSG.success();
               returnMSG.add("user",youngUser);
               return returnMSG;
           }else{
                returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
                returnMSG.setMsg("密码输入错误");
                return returnMSG;
           }
    }

    @PostMapping("/resetPasswordY")
    @ApiOperation("用户重置密码:phone,verifyCode,newpassword,confirmpassword")
    public returnMSG loginInCode(@RequestParam("phone") String phone,@RequestParam("verifyCode") String verifyCode,@RequestParam("newpassword") String newpassword,@RequestParam("confirmpassword") String confirmPassword){
        //从redis里面取到验证码
        if(!newpassword.equals(confirmPassword)){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入一致的密码");
            return returnMSG;
        }
        String code = redisTemplate.opsForValue().get(phone);
        if(yuserService.loginByVerifyCode(verifyCode,code)){
            String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
            boolean flag=newpassword.matches(reg);
            if(flag){
                returnMSG returnMSG= labelsystem.utils.returnMSG.success();
                YoungUser youngUser=yuserService.queryInfoBytel(phone);
                youngUser.setPassword(newpassword);
                yuserService.updateInfo(youngUser);
                returnMSG.add("user",youngUser);
                return returnMSG;
            }else{
                returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
                returnMSG.setMsg("重置密码不符合格式");
                return returnMSG;
            }
        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("验证码错误");
            return returnMSG;
        }
    }

    @PostMapping("/registerY")
    @ApiOperation("用户注册:name,phone,password,gender")
    public returnMSG registerUser(@RequestParam("name")String name,@RequestParam("phone") String telephone,@RequestParam("password") String password,@RequestParam("gender") String gender){
        String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
        boolean flag=password.matches(reg);
        if(!flag){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入合法的密码格式");
            return returnMSG;
        }else{
           YoungUser youngUser=yuserService.register(name,telephone,password,gender);
           returnMSG returnMSG= labelsystem.utils.returnMSG.success();
           returnMSG.add("user",youngUser);
           return returnMSG;
        }

    }

    @GetMapping("/queryOrderY")
    @ApiOperation("用户查询父母订单:yid")
    public returnMSG queryOrder(@RequestParam("yid") String id){
        List<Order> orders=yuserService.queryOOrders(Long.parseLong(id));
        returnMSG returnMS=returnMSG.success();
        returnMS.add("orders",orders);
        return  returnMS;
    }

    @GetMapping("/queryDoctorsY")
    @ApiOperation("用户查询医生")
    public returnMSG  queryDoctors(){
       List<Doctor> list=yuserService.queryDoctors();
       returnMSG returnMSG= labelsystem.utils.returnMSG.success();
       returnMSG.add("doctors",list);
       return returnMSG;
    }

   @GetMapping("/queryDoctorByIdY")
   @ApiOperation("id查询医生：doctorID")
    public returnMSG queryDoctorById(@RequestParam("doctorID") String doctorID){
        Doctor doctor=yuserService.queryDoctorById(Long.parseLong(doctorID));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("doctor",doctor);
        return returnMSG;
   }

   @GetMapping("/queryDoctorBySpecifyY")
   @ApiOperation("专业查询医生：specify")
    public returnMSG queryDoctorBySpecify(@RequestParam("specify") String specify){
       List<Doctor> doctors=yuserService.queryDoctorBySpecify(specify);
       returnMSG returnMSG= labelsystem.utils.returnMSG.success();
       returnMSG.add("doctors",doctors);
       return returnMSG;
   }

   @GetMapping("/queryDeviceCheckY")
   @ApiOperation("用户id查询硬件检测:yid")
   public returnMSG queryDeviceCheck(@RequestParam("yid") String yid){
        List<Device> devices=yuserService.queryDeviceCheck(Long.parseLong(yid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("devices", devices);
        return returnMSG;
   }
   @GetMapping("/queryMonitorY")
   @ApiOperation("用户id查询父母检测:yid")
   public returnMSG queryMonitor(@RequestParam("yid") String yid){
        List<monitorHandle> monitors=yuserService.queryMonitor(Long.parseLong(yid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("monitor",monitors);
        return returnMSG;
   }
   @GetMapping("/queryVolunteersY")
   @ApiOperation("查询护工")
   public returnMSG queryVolunteers(){
       List<Volunteer> volunteers=yuserService.queryVolunteers();
       returnMSG returnMSG= labelsystem.utils.returnMSG.success();
       returnMSG.add("volunteers",volunteers);
       return returnMSG;
   }

   @PostMapping("/bindOlderY")
   @ApiOperation("绑定老年人:yid,name,address,age,gender,status,phone,relationship")
   public returnMSG bindOlder(@RequestParam("yid") String yid,
                              @RequestParam("name") String name,
                              @RequestParam("address") String address,
                              @RequestParam("age") String age,
                              @RequestParam("gender") String gender,
                              @RequestParam("status") @ApiParam("父母健康综述") String status,
                              @RequestParam("phone") String phone,
                              @RequestParam("relationship") String relationship){
          boolean flag=yuserService.registerOlder(Long.parseLong(yid),name,address,Integer.parseInt(age),gender,status,Long.parseLong(phone),relationship);
          if(flag){
              return returnMSG.success();
          }else{
              return returnMSG.fail();
          }
   }

   @GetMapping("/queryCheckY")
   @ApiOperation("用户id查询父母检查结果:yid")
    public returnMSG queryCheck(@RequestParam("yid") String yid){
        List<Check> checks=yuserService.queryCheckById(Long.parseLong(yid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("checks",checks);
        return returnMSG;
   }
   @GetMapping("/queryVolunteerByIdY")
   @ApiOperation("id查询护工:vid")
   public returnMSG queryVolunteerById(@RequestParam("vid") String vid){
        Volunteer volunteer=yuserService.queryServiceById(Long.parseLong(vid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("volunteer",volunteer);
        return returnMSG;
   }

   @PostMapping("/addMonitorY")
   @ApiOperation("添加检测记录:name,phone,medicine,cycle")
    public returnMSG addMonitor(@RequestParam("name") String name,@RequestParam("phone") String phone,@RequestParam("medicine") String medicine,@RequestParam("cycle") @ApiParam("用药周期") String cycle){
        boolean flag=yuserService.AddMonitor(name,phone,medicine,Integer.parseInt(cycle));
        if(flag==true){
            return returnMSG.success();
        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("该时间段已经添加监测数据，请勿重复添加");
            return returnMSG;

        }
   }

   @PostMapping("/updatePasswordY")
   @ApiOperation("更改密码:phone,password")
    public returnMSG updatePassword(@RequestParam("phone") String phone,@RequestParam("password") String password){
        YoungUser youngUser=yuserService.queryInfoBytel(phone);
        youngUser.setPassword(password);
        boolean flag=yuserService.updateInfo(youngUser);
        if(flag){
            return returnMSG.success();
        }else{
            return returnMSG.fail();
        }
   }

   @GetMapping("/deleteMonitorY")
   @ApiOperation("删除检测记录:oid")
    public returnMSG deleteMonitorY(@RequestParam("oid") @ApiParam("老人id") String oid){
        boolean flag=yuserService.deleteMonitor(Long.parseLong(oid));
        if(flag){
            return returnMSG.success();
        }else{
            return returnMSG.fail();
        }
   }
}
